package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.GIDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private CheckGroupDao checkGroupDao;


    @Override
    public void add(CheckItem checkItem) {
            checkItemDao.add(checkItem);
    }

    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        Page<CheckItem> page = checkItemDao.queryPage(queryString);
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        return new PageResult(total,rows);

    }

    @Override
    public CheckItem findone(int id) {
        CheckItem checkItem = checkItemDao.findone(id);
        return checkItem;
    }

    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }

    @Override
    public Result delete(int id) {
        List<CheckGroup> group = checkGroupDao.findGroup(id);
        if(group.size()>0){
            return new Result(false,"此检查项被使用，无法被删除");
        }else{
            try {
                checkItemDao.delete(id);
            } catch (Exception e) {
                e.printStackTrace();
                return  new Result(false,"此检查项无法被删除");
            }
            return new Result(true,"删除成功");
        }

    }


    public List<CheckItem> queryItemlist(){
        String thing=null;
        Page<CheckItem> checkItems = checkItemDao.queryPage(thing);
        return checkItems;
    }
}